import * as THREE from 'three'
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls.js'
import { Apply } from './Apply/Apply'
import { CSS3DRenderer } from 'three/examples/jsm/renderers/CSS3DRenderer'
// import States from 'three/examples/jsm/libs/stats.module'
const CONTROLS_JSON = { // 控制器配置
  minDistance: 0,// 移动相机最近位置
  maxDistance: 3000, // 移动相机最远位置
  minPolarAngle: 0, // 最小仰视角
  maxPolarAngle: Math.PI / 2.5, // 最大俯视角
  enableDamping: false, // 启用阻尼
  autoRotate: true, // 自动旋转
  autoRotateSpeed: 1,// 自动旋转速度
  zoomSpeed: 1, // 鼠标滚轮滚动时放大缩小尺寸大小
  enablePan: false, // 关闭平移功能
}
export class DutuJS extends Apply {
  constructor({
    dom,
    cameraPosition = { x: 2000, y: 2000, z: 2000 },
    addAxesHelper = false,
    addAmbientLight = true,
    addSunShine = true,
    sunRadius = 450000,
    sunRange = 0,
    LIGHTNAME = "Light_",
    background = '#fff',
    controlsJson = CONTROLS_JSON,
    ambientLight_strength = 0.01
  }) {
    super();
    this.dom = dom
    this.cameraPosition = cameraPosition
    this.addAxesHelper = addAxesHelper
    this.controlsJson = controlsJson
    this.addAmbientLight = addAmbientLight
    this.addSunShine = addSunShine
    this.sunRadius = sunRadius
    this.sunRange = sunRange
    this.LIGHTNAME = LIGHTNAME
    this.background = background
    this.otherGroup = null
    this.state = null
    this.cameraRoam = false // 相机漫游开关
    this.roamPath = false // 相机漫游路线
    this.roamIndex = 0 // 相机漫游点位记录
    this.ambientLight_strength = ambientLight_strength // 环境光照强度
    this.init()
  }
  /**
   * 初始化
   */
  init () {
    // this.state = new States()
    // this.dom.appendChild(this.state.dom)
    this.scene = new THREE.Scene()
    this.camera = new THREE.PerspectiveCamera(45, this.dom.clientWidth / this.dom.clientHeight, 0.1, 999999)
    this.renderer = new THREE.WebGLRenderer({
      antialias: true,
      logarithmicDepthBuffer: true, // 防止叠面闪烁问题
      alpha: true
    })
    this.camera.position.set(this.cameraPosition.x, this.cameraPosition.y, this.cameraPosition.z)
    // this.renderer.setClearColor(this.background, 1.0)
    this.renderer.setSize(this.dom.clientWidth, this.dom.clientHeight)
    this.dom.appendChild(this.renderer.domElement)
    // 雾化效果
    this.scene.fog = new THREE.FogExp2(0xffffff, 0.0001);
    // 坐标轴辅助
    if (this.addAxesHelper) {
      var axesHelper = new THREE.AxesHelper(10000);
      this.scene.add(axesHelper);
    }
    // 除了场景外的所有模型加入到此组，为了在模型显隐的时候找模型会更快，避开了在场景模型内的遍历
    this.otherGroup = new THREE.Group()
    this.scene.add(this.otherGroup)
    // 控制器初始化
    this.controls = new OrbitControls(this.camera, this.dom)
    this.controls = Object.assign(this.controls, this.controlsJson)
    // 光源初始化
    if (this.addAmbientLight) {
      let ambientLight = new THREE.AmbientLight(0xffffff, this.ambientLight_strength) // 白天环境光
      // let ambientLight = new THREE.AmbientLight(0x1e2944, 0.01) // 夜晚环境光
      ambientLight.name = 'baseAmbientLight'
      this.scene.add(ambientLight)
    }

    this.renderer.shadowMap.enabled = true; // 打开阴影
    this.renderer.shadowMap.type = THREE.PCFSoftShadowMap;
    // 太阳光初始化
    if (this.addSunShine) {
      let sunShine = new THREE.DirectionalLight(0xffffff, 0.5) // 平行光
      sunShine.position.set(100, 100, 100)
      sunShine.name = 'sunShine'
      this.scene.add(sunShine)
      let sunShine1 = new THREE.DirectionalLight(0xffffff, 0.5) // 平行光
      sunShine1.position.set(-100, 100, -100)
      // this.scene.add(sunShine1)
      let sunShine2 = new THREE.DirectionalLight(0xffffff, 0.5) // 平行光
      sunShine2.position.set(-100, 100, 100)
      this.scene.add(sunShine2)
      let sunShine3 = new THREE.DirectionalLight(0xffffff, 0.5) // 平行光
      sunShine3.position.set(100, 100, -100)
      // this.scene.add(sunShine3)



      let sunShine4 = new THREE.DirectionalLight(0xffffff, 0.5) // 平行光
      sunShine4.position.set(100, -100, 100)
      this.scene.add(sunShine4)
      let sunShine5 = new THREE.DirectionalLight(0xffffff, 0.5) // 平行光
      sunShine5.position.set(-100, -100, -100)
      // this.scene.add(sunShine5)
      let sunShine6 = new THREE.DirectionalLight(0xffffff, 0.5) // 平行光
      sunShine6.position.set(-100, -100, 100)
      this.scene.add(sunShine6)
      let sunShine7 = new THREE.DirectionalLight(0xffffff, 0.5) // 平行光
      sunShine7.position.set(100, -100, -100)
      // this.scene.add(sunShine7)
    }
    // label标签层的渲染器
    this.css3DRenderer = new CSS3DRenderer()
    this.css3DRenderer.setSize(this.dom.offsetWidth, this.dom.offsetHeight)
    this.css3DRenderer.domElement.style.position = 'absolute'
    this.css3DRenderer.domElement.style.top = 0
    this.css3DRenderer.domElement.style.pointerEvents = "none"
    this.dom.appendChild(this.css3DRenderer.domElement)
    // 屏幕尺寸变动
    window.onresize = () => {
      this.camera.aspect = this.dom.offsetWidth / this.dom.offsetHeight
      this.renderer.setSize(this.dom.offsetWidth, this.dom.offsetHeight) // 重新获取
      this.camera.updateProjectionMatrix()
      this.css3DRenderer.setSize(this.dom.offsetWidth, this.dom.offsetHeight) // 重新获取
    }
    this.dom.addEventListener('click', () => {
      console.log('controls', this.controls.target)
      console.log('camera', this.camera.position)
    })
  }
  /**
   * 渲染
   */
  render () {
    if (this.cameraRoam) {
      this.cameraRoamTo()
    }
    if (this.state) {
      this.state.update()
    }
    this.renderer.render(this.scene, this.camera)
    this.controls.update();
  }
  /** 相机漫游动画执行 */
  cameraRoamTo () {
    let position = this.roamPath[this.roamIndex]
    this.camera.position.x = position.x
    this.camera.position.y = position.y
    this.camera.position.z = position.z
    this.camera.lookAt(this.roamPath[parseInt(this.roamPath.length / 6)])
    if (this.roamIndex + 3 > this.roamPath.length) {
      this.roamIndex = 0
    } else {
      this.roamIndex++
    }
  }
  closeRoam () {
    this.cameraRoam = false
  }
}
